﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HSFB.Lib;
using System.Diagnostics;
using System.IO;

namespace HSFB.Console
{
    public static class Test
    {
        /// <summary>
        /// Range: 1 - 100
        /// Labels: Default
        /// Output: Console
        /// </summary>
        public static void Default(string title)
        {

            SetTitle(title);

            //Call Fizz Buzz
            Lib.FB.Run(1, 100);

            SetFooter(title);
        }

        /// <summary>
        /// Range: -100 - 100
        /// Labels: Default
        /// Output: Console
        /// </summary>
        public static void Default_Range(string title)
        {
           
            SetTitle(title);

            //Call Fizz Buzz
            Lib.FB.Run(-100, 100);

            SetFooter(title);
        }

        /// <summary>
        /// Range: 1 - 100
        /// Labels: User Input
        /// Output: Console
        /// </summary>
        public static void Default_Labels(string title)
        {
            SetTitle(title);

            System.Console.Write("Enter Label for divisable by 3 (then press enter)");
            var label_3 = System.Console.ReadLine();

            System.Console.Write("Enter Label for divisable by 5 (then press enter)");
            var label_5 = System.Console.ReadLine();

            //Call Fizz Buzz
            Lib.FB.Run(1, 100, label_3, label_5);

            SetFooter(title);
        }

        /// <summary>
        /// Range: 1 - 100
        /// Labels: Custom
        /// Output: Log file
        /// </summary>
        public static void Trace_LogFile(string title)
        {
            var fileName = "trace.log";
            var subtitle = string.Format("Output to Log File \"{0}\" as set on config.file", fileName);

            SetTitle(title + Environment.NewLine + subtitle);

            System.Console.WriteLine("Delete previous log file");
            File.Delete(fileName);

            var trace = Trace.Listeners["LogFile"];

            //Call Fizz Buzz
            Lib.FB.Run(trace, 1, 100, "Works?", "For real!");
            trace.Flush();
            trace.Dispose();

            var info = new FileInfo(fileName);
            System.Console.WriteLine("File {0} FileSize: {1} File contents:", info.FullName, info.Length);
            System.Console.Write(File.ReadAllText(fileName));

            SetFooter(title);
        }

        /// <summary>
        /// Range: 1 - 100
        /// Labels: Default
        /// Output: Trace console
        /// </summary>
        public static void Trace_Console(string title)
        {

            SetTitle(title);

            var trace = Trace.Listeners["Console"];
            //Call Fizz Buzz
            Lib.FB.Run(trace, 1, 100);
            trace.Flush();

            SetFooter(title);
        }

        /// <summary>
        /// Range: 1 - 100
        /// Labels: Default
        /// Output: string collection
        /// </summary>
        public static void Trace_Collection(string title)
        {

            SetTitle(title);

            var values = new List<string>();

            //Call Fizz Buzz
            Lib.FB.Run(values, 1, 100);

            System.Console.WriteLine("Number of items in collection: {0}", values.Count());
            values.ForEach(value => System.Console.WriteLine(value));

            SetFooter(title);
        }

        /// <summary>
        /// Range: 1 - 100
        /// Labels: Default
        /// Output: String Writer 
        /// </summary>
        public static void Trace_TextWriter(string title)
        {

            SetTitle(title);

            var sb = new StringBuilder();
            var writer = new StringWriter(sb);

            //Call Fizz Buzz
            Lib.FB.Run(writer, 1, 100);

            var values = sb.ToString();
            System.Console.WriteLine("StringWriter size: {0} chars", sb.Length);
            System.Console.Write(values);

            SetFooter(title);
        }

        /// <summary>
        /// Range: 1 - 100
        /// Labels: Default
        /// Output: Stream, Unicode 
        /// </summary>
        public static void Default_Stream(string title)
        {

            SetTitle(title);

            using (var stream = new MemoryStream())
            {
                Lib.FB.Run(stream, Encoding.Unicode, 1, 100);

                stream.Position = 0;

                using (var reader = new StreamReader(stream, Encoding.Unicode))
                {
                    var values = reader.ReadToEnd();
                    System.Console.WriteLine("MemoryStream size: {0} bytes", stream.Length);
                    System.Console.Write(values);
                }
            }

            SetFooter(title);
        }

        /// <summary>
        /// Range: 1 - 100
        /// Labels: Default
        /// Output: FBWriter, Console
        /// </summary>
        public static void Default_FBWriter(string title)
        {

            SetTitle(title);

            var writer = new FBWriter();

            Lib.FB.Run(writer, 1, 100);

            var values = writer.ReadAll();
            System.Console.WriteLine("FBWriter size: {0} chars", values.Length);
            System.Console.Write(values);

            SetFooter(title);
        }

        public static void Exit(string title)
        {
            Environment.Exit(0);
        }

        static void SetTitle(string title)
        {
            System.Console.WriteLine(title);
        }

        static void SetFooter(string title)
        {
            System.Console.WriteLine("End of {0}.", title);
        }
    }
}
